package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.media.*;
    import flash.net.*;
    import flash.text.*;
    import com.greensock.*;

    public class ProjVideoShell extends Sprite
    {
        var dur:Number;
        public var thebar:MovieClip;
        var videoWidth:Number;
        var fadeSpeed:Number;
        var bg:Sprite;
        var _scrubbing:Boolean;
        var displayHours:Boolean;
        public var video:Video;
        var videoHeight:Number;
        var captionFormat:TextFormat;
        var fieldFormat:TextFormat;
        var myRoot:MovieClip;
        var ns:NetStream;
        var nc:NetConnection;
        var styler:StyleSheet;

        public function ProjVideoShell(param1:MovieClip, param2:Number) : void
        {
            var _loc_3:Object = null;
            myRoot = param1;
            fadeSpeed = param2;
            bg = new Sprite();
            addChild(bg);
            bg.graphics.beginFill(16777215, 1);
            bg.graphics.drawRect(0, 0, 1, 1);
            bg.graphics.endFill();
            addChildAt(bg, 0);
            video.alpha = 0;
            nc = new NetConnection();
            nc.connect(null);
            ns = new NetStream(nc);
            ns.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
            _loc_3 = new Object();
            _loc_3.onMetaData = onMetaData;
            ns.client = _loc_3;
            video.attachNetStream(ns);
            thebar.progress.scaleX = 0;
            thebar.loaded.addEventListener(MouseEvent.CLICK, seekTo);
            thebar.loaded.buttonMode = true;
            thebar.loaded.useHandCursor = false;
            thebar.progress.mouseEnabled = false;
            thebar.scrubber.buttonMode = true;
            _scrubbing = false;
            thebar.scrubber.addEventListener(MouseEvent.MOUSE_DOWN, scrubberPress);
            thebar.closeBtn.buttonMode = true;
            thebar.closeBtn.addEventListener(MouseEvent.MOUSE_OVER, closeOver);
            thebar.closeBtn.addEventListener(MouseEvent.MOUSE_OUT, closeOut);
            thebar.closeBtn.addEventListener(MouseEvent.CLICK, closeClick);
            thebar.playPause.buttonMode = true;
            thebar.playPause.addEventListener(MouseEvent.MOUSE_OVER, playPauseOver);
            thebar.playPause.addEventListener(MouseEvent.MOUSE_OUT, playPauseOut);
            thebar.playPause.addEventListener(MouseEvent.CLICK, playPauseClick);
            thebar.lbBtn.visible = false;
            thebar.lbBtn.buttonMode = false;
            displayHours = true;
            fieldFormat = new TextFormat();
            fieldFormat.font = new HelveticaLightEmbed().fontName;
            fieldFormat.leading = 0;
            fieldFormat.size = 14;
            fieldFormat.letterSpacing = 0;
            captionFormat = new TextFormat();
            captionFormat.font = new HelveticaLightEmbed().fontName;
            captionFormat.leading = -3;
            captionFormat.size = 14;
            captionFormat.letterSpacing = 0;
            thebar.timecode.autoSize = "left";
            thebar.timecode.defaultTextFormat = fieldFormat;
            thebar.btnField.autoSize = "left";
            thebar.btnField.defaultTextFormat = fieldFormat;
            buildStyles();
            thebar.caption.autoSize = "left";
            thebar.caption.multiline = true;
            thebar.caption.wordWrap = true;
            thebar.caption.styleSheet = styler;
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            doResize();
            return;
        }

        function closeOver(event:MouseEvent) : void
        {
            TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.hotColor, overwrite:true});
            thebar.btnField.text = "Close";
            return;
        }

        public function fadeOutShell() : void
        {
            TweenMax.to(this, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                visible = false;
                killVideo();
                return;
            }
            });
            return;
        }

        function scrubberPress(event:MouseEvent) : void
        {
            var _loc_2:Rectangle = null;
            _scrubbing = true;
            _loc_2 = new Rectangle(0, 0, thebar.track.width, 0);
            thebar.scrubber.startDrag(false, _loc_2);
            stage.addEventListener(MouseEvent.MOUSE_UP, scrubberRelease);
            return;
        }

        function doResize() : void
        {
            bg.y = Math.round(myRoot.headerH + 2 * (myRoot.unitH + myRoot.gutterH));
            bg.width = myRoot.gridCurrW;
            bg.height = 4 * (myRoot.unitH + myRoot.gutterH);
            if (dur)
            {
                var _loc_1:* = myRoot.unitW / 75;
                thebar.caption.scaleY = myRoot.unitW / 75;
                thebar.caption.scaleX = _loc_1;
                var _loc_1:* = myRoot.unitW / 75;
                thebar.btnField.scaleY = myRoot.unitW / 75;
                thebar.btnField.scaleX = _loc_1;
                var _loc_1:* = myRoot.unitW / 75;
                thebar.timecode.scaleY = myRoot.unitW / 75;
                thebar.timecode.scaleX = _loc_1;
                thebar.caption.y = Math.round(23 + 29 * (myRoot.unitW / 75));
                video.width = videoWidth;
                video.height = videoHeight;
                if (video.height >= Math.round(3 * (myRoot.unitH + myRoot.gutterH)))
                {
                    video.height = Math.round(3 * (myRoot.unitH + myRoot.gutterH));
                    video.width = Math.round(videoWidth * video.height / videoHeight);
                }
                video.x = Math.round((bg.width - video.width) / 2);
                video.y = Math.round(bg.y - myRoot.gutterH + (bg.height - (video.height + thebar.caption.height + 48 * (myRoot.unitW / 75))) / 2);
                trace("thebar.caption.height " + thebar.caption.height);
                thebar.x = video.x;
                thebar.y = Math.round(video.y + video.height + 2);
                thebar.track.width = video.width;
                thebar.loaded.width = thebar.track.width * (ns.bytesLoaded / ns.bytesTotal);
                if (dur)
                {
                    if (_scrubbing == false)
                    {
                        thebar.progress.width = thebar.track.width * (ns.time / dur);
                        thebar.scrubber.x = thebar.progress.width;
                    }
                    else
                    {
                        thebar.progress.width = thebar.scrubber.x;
                        ns.seek(thebar.mouseX / thebar.track.width * dur);
                    }
                }
                thebar.timecode.x = Math.round(thebar.track.width - thebar.timecode.width + 2);
            }
            return;
        }

        function playPauseOver(event:MouseEvent) : void
        {
            TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.hotColor, overwrite:true});
            if (thebar.playPause.pauseBtn.alpha == 1)
            {
                thebar.btnField.text = "Pause";
            }
            else
            {
                thebar.btnField.text = "Play";
            }
            return;
        }

        public function loadVideo(param1:String, param2:String)
        {
            thebar.playPause.pauseBtn.alpha = 1;
            thebar.playPause.playBtn.alpha = 0;
            thebar.caption.htmlText = param2;
            ns.play(myRoot.path + param1);
            addEventListener(Event.ENTER_FRAME, loop);
            return;
        }

        function closeOut(event:MouseEvent) : void
        {
            TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
            thebar.btnField.text = "";
            return;
        }

        function fadeInVideo() : void
        {
            video.clear();
            TweenMax.to(video, fadeSpeed, {alpha:1, overwrite:true});
            TweenMax.to(thebar, fadeSpeed, {alpha:1, overwrite:true});
            return;
        }

        public function fadeOutImage(param1:int) : void
        {
            return;
        }

        function seekTo(event:Event) : void
        {
            ns.seek(thebar.mouseX / thebar.track.width * dur);
            trace("seek to " + thebar.mouseX / thebar.track.width);
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function onMetaData(param1:Object)
        {
            trace("onMetaData");
            dur = param1.duration;
            videoWidth = param1.width;
            videoHeight = param1.height;
            doResize();
            fadeInVideo();
            return;
        }

        function fadeOutVideo() : void
        {
            TweenMax.to(video, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                killVideo();
                return;
            }
            });
            TweenMax.to(thebar, fadeSpeed, {alpha:0, overwrite:true});
            return;
        }

        function closeClick(event:MouseEvent) : void
        {
            if (myRoot.pathNames[1] == "press" && myRoot.pathNames[2] == "video")
            {
                ns.pause();
                ns.close();
                myRoot.clickPathNames = ["practice", "press", "video", "1"];
                myRoot.updateAddress("/" + "practice" + "/" + "press" + "/" + "video" + "/" + "1" + "/");
            }
            else
            {
                ns.pause();
                fadeOutVideo();
            }
            return;
        }

        function playPauseClick(event:MouseEvent) : void
        {
            if (thebar.playPause.pauseBtn.alpha == 1)
            {
                ns.pause();
                thebar.playPause.pauseBtn.alpha = 0;
                thebar.playPause.playBtn.alpha = 1;
                thebar.btnField.text = "Play";
            }
            else if (thebar.playPause.playBtn.alpha == 1)
            {
                ns.resume();
                thebar.playPause.pauseBtn.alpha = 1;
                thebar.playPause.playBtn.alpha = 0;
                thebar.btnField.text = "Pause";
            }
            return;
        }

        public function deepLinkClose() : void
        {
            ns.pause();
            ns.close();
            return;
        }

        function loop(event:Event) : void
        {
            thebar.track.width = video.width;
            thebar.loaded.width = thebar.track.width * (ns.bytesLoaded / ns.bytesTotal);
            if (dur)
            {
                if (_scrubbing == false)
                {
                    thebar.progress.width = thebar.track.width * (ns.time / dur);
                    thebar.scrubber.x = thebar.progress.width;
                }
                else
                {
                    thebar.progress.width = thebar.scrubber.x;
                    ns.seek(thebar.mouseX / thebar.track.width * dur);
                }
            }
            thebar.timecode.text = getTimecode(thebar.scrubber.x / thebar.track.width * dur);
            thebar.timecode.x = Math.round(thebar.track.width - thebar.timecode.width + 2);
            return;
        }

        function getTimecode(param1) : String
        {
            var _loc_2:* = undefined;
            var _loc_3:* = undefined;
            var _loc_4:* = undefined;
            var _loc_5:* = undefined;
            var _loc_6:* = undefined;
            _loc_2 = param1;
            _loc_3 = Math.floor(_loc_2 / 60);
            if (displayHours)
            {
                _loc_6 = Math.floor(_loc_3 / 60);
            }
            _loc_4 = Math.round(_loc_2 - _loc_3 * 60);
            if (_loc_4 == 60)
            {
                _loc_4 = "00";
                _loc_3 = _loc_3 + 1;
                if (_loc_3 < 10)
                {
                    _loc_3 = "0" + _loc_3;
                }
                if (_loc_6 < 10)
                {
                    _loc_6 = "0" + _loc_6;
                }
            }
            else
            {
                if (_loc_4 < 10)
                {
                    _loc_4 = "0" + _loc_4;
                }
                if (_loc_3 < 10)
                {
                    _loc_3 = "0" + _loc_3;
                }
                if (_loc_6 < 10)
                {
                    _loc_6 = "0" + _loc_6;
                }
            }
            _loc_5 = _loc_6 + ":" + _loc_3 + ":" + _loc_4;
            return _loc_5;
        }

        function onStatus(param1:Object) : void
        {
            trace("onSTATUS");
            if (param1.info.code == "NetStream.Play.Start" || param1.info.code == "NetStream.Buffer.Full")
            {
            }
            else if (param1.info.code == "NetStream.Buffer.Empty")
            {
            }
            else if (param1.info.code == "NetStream.Play.Stop" && _scrubbing == false)
            {
                trace("stop " + ns.time / dur);
                ns.pause();
                ns.seek(0);
                thebar.playPause.pauseBtn.alpha = 0;
                thebar.playPause.playBtn.alpha = 1;
            }
            return;
        }

        function killVideo()
        {
            ns.close();
            dispatchEvent(new ClickCloseEvent());
            return;
        }

        function playPauseOut(event:MouseEvent) : void
        {
            TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
            thebar.btnField.text = "";
            return;
        }

        function scrubberRelease(event:MouseEvent) : void
        {
            thebar.scrubber.stopDrag();
            _scrubbing = false;
            stage.removeEventListener(MouseEvent.MOUSE_UP, scrubberRelease);
            return;
        }

        public function fadeInShell() : void
        {
            visible = true;
            TweenMax.to(this, fadeSpeed, {alpha:1, overwrite:true});
            return;
        }

        function buildStyles() : void
        {
            var _loc_1:* = undefined;
            var _loc_2:* = undefined;
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_6:Object = null;
            var _loc_7:* = undefined;
            var _loc_8:Object = null;
            _loc_1 = new HelveticaLightEmbed().fontName;
            _loc_2 = new HelveticaBoldEmbed().fontName;
            styler = new StyleSheet();
            _loc_3 = new Object();
            _loc_3.color = myRoot.coldColorHTML;
            _loc_3.fontFamily = _loc_1;
            _loc_3.leading = -4;
            _loc_4 = new Object();
            _loc_4.color = myRoot.coldColorHTML;
            _loc_4.textDecoration = "underline";
            _loc_5 = new Object();
            _loc_5.color = myRoot.hotColorHTML;
            _loc_5.textDecoration = "underline";
            _loc_6 = new Object();
            _loc_6.color = myRoot.coldColorHTML;
            _loc_6.fontFamily = _loc_2;
            _loc_7 = new HelveticaItalicEmbed().fontName;
            _loc_8 = new Object();
            _loc_8.color = myRoot.coldColorHTML;
            _loc_8.fontFamily = _loc_7;
            styler.setStyle(".myItalic", _loc_8);
            styler.setStyle("p", _loc_3);
            styler.setStyle("a:link", _loc_4);
            styler.setStyle("a:hover", _loc_5);
            styler.setStyle(".mybold", _loc_6);
            return;
        }

    }
}
